package com.skyolder.milkbee.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Ware;
import com.skyolder.milkbee.service.WareService;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Ware.
 */
@RestController
@RequestMapping("/api")
public class WareResource {

    private final Logger log = LoggerFactory.getLogger(WareResource.class);
        
    @Inject
    private WareService wareService;

    /**
     * POST  /wares : Create a new ware.
     *
     * @param ware the ware to create
     * @return the ResponseEntity with status 201 (Created) and with body the new ware, or with status 400 (Bad Request) if the ware has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/wares")
    @Timed
    public ResponseEntity<Ware> createWare(@RequestBody Ware ware) throws URISyntaxException {
        log.debug("REST request to save Ware : {}", ware);
        if (ware.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("ware", "idexists", "A new ware cannot already have an ID")).body(null);
        }
        Ware result = wareService.save(ware);
        return ResponseEntity.created(new URI("/api/wares/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("ware", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /wares : Updates an existing ware.
     *
     * @param ware the ware to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated ware,
     * or with status 400 (Bad Request) if the ware is not valid,
     * or with status 500 (Internal Server Error) if the ware couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/wares")
    @Timed
    public ResponseEntity<Ware> updateWare(@RequestBody Ware ware) throws URISyntaxException {
        log.debug("REST request to update Ware : {}", ware);
        if (ware.getId() == null) {
            return createWare(ware);
        }
        Ware result = wareService.save(ware);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("ware", ware.getId().toString()))
            .body(result);
    }

    /**
     * GET  /wares : get all the wares.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of wares in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/wares")
    @Timed
    public ResponseEntity<List<Ware>> getAllWares(@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Wares");
        Page<Ware> page = wareService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/wares");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /wares/:id : get the "id" ware.
     *
     * @param id the id of the ware to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the ware, or with status 404 (Not Found)
     */
    @GetMapping("/wares/{id}")
    @Timed
    public ResponseEntity<Ware> getWare(@PathVariable Long id) {
        log.debug("REST request to get Ware : {}", id);
        Ware ware = wareService.findOne(id);
        return Optional.ofNullable(ware)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /wares/:id : delete the "id" ware.
     *
     * @param id the id of the ware to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/wares/{id}")
    @Timed
    public ResponseEntity<Void> deleteWare(@PathVariable Long id) {
        log.debug("REST request to delete Ware : {}", id);
        wareService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("ware", id.toString())).build();
    }

}
